﻿package gui.hug;

  import flash.display.Sprite;
  import flash.text.TextField;
  import flash.events.MouseEvent;
    
  /**
   * @author Alexey Kharkov
   * @modified by Natalia Ukhorskaya
   */
  class HUGRadioButton extends LabelButton
  {
    private var group:HUGRadioButtonsGroup;
    private var idx:UInt;
    private var isChecked:Bool;
    private var isOver:Bool;
    private var img:RadioButtonImg;
	
	//public var checked(getCheck, setCheck): Bool;
	public var index(getIndex, null): UInt;
    
    public function new( label:String, idx:UInt, group:HUGRadioButtonsGroup, x:Int, y:Int )
    {
      super( label, 19, -2 );
		
	  isChecked = false;
      isOver = false;
      this.x = x;
      this.y = y;
      this.group = group;
      this.idx = idx;
      
      // Transparent BackGround
      Utils.fillRect( this, 0, 0, width, 15, 0, 0 );

      //
      img = new RadioButtonImg();
      addChild( img );
      
      reDraw();

      addEventListener( MouseEvent.MOUSE_OVER, onOver  );
      addEventListener( MouseEvent.MOUSE_OUT , onOut   );
      addEventListener( MouseEvent.MOUSE_DOWN, onClick );
      
      buttonMode = true;
      mouseChildren = false;
    }
    
    public function getIndex():UInt
    {
      return idx;
    }
    
    public function getCheck():Bool
    {
      return isChecked;
    }
    
    public function setCheck( b:Bool ): Bool
    {
      setChecked( b );
      group.setSelectedIndex(index);
	  return b;
    }
    
    // ----------------------------------------------------------------------- internal methods
    public function setChecked( b:Bool )
    {
      isChecked = b;
      reDraw();
    }
	
    // ----------------------------------------------------------------------- private methods
    private function reDraw()
    {
      if ( isChecked )
        img.gotoAndStop( isOver ? 3 : 1 );
      else
        img.gotoAndStop( isOver ? 4 : 2 );
    }
    
    // ----------------------------------------------------------------------- Event handlers
    private function onOver( e:MouseEvent )
    {
      isOver = true;
      reDraw();
    }

    private function onOut( e:MouseEvent )
    {
      isOver = false;
      reDraw();
    }

    private function onClick( e:MouseEvent )
    {
      setCheck(true);
    }
  }
